<?php

namespace App\Http\Controllers\system;

use App\Http\Controllers\Controller;
use App\Models\admin\AdminUserModel;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use lxs\api\ApiResponse;

class AdminUserController extends Controller
{

    /**
     * 用户信息
     */
    public function getSystemUserInfo(Request $request)
    {

        $uid = $request->system_uid;

        $user = AdminUserModel::where('id', $uid)->first()->toArray();

        return ApiResponse::success('获取用户信息成功', $user);

    }


    /**
     * 用户修改密码
     */
    public function systemUserPasswordSubmit(Request $request)
    {

        $uid = $request->system_uid;
        $data = $request->input();

        $user = AdminUserModel::where('id', $uid)->first();

        if (!Hash::check($data['old_pass'], $user->password)) {
            return ApiResponse::error('旧密码错误');
        }

        $user->password = Hash::make($data['new_pass']);
        $user->save();
        return ApiResponse::success('修改密码成功');
    }


    /**
     * 获取管理员列表页面数据
     */
    public function getAdminListData()
    {

        $data = request()->input();

        $query = AdminUserModel::with(['role:id,role_name'])->select('*');

        $list = $query->orderBy('id', 'asc')->paginate($data['pageSize']);

        return ApiResponse::apiResponseData(200, '获取管理员列表页面数据成功', [
            'list' => $list
        ]);
    }


    /**
     * 管理员表单提交
     */
    public function systemUserFormSubmit()
    {

        $data = request()->input();

        if (!isset($data['id'])) {

            if (empty($data['password'])) {
                return ApiResponse::error('密码不能为空');
            }

            if (empty($data['username'])) {
                return ApiResponse::error('用户名不能为空');
            }

            if (AdminUserModel::where('username', $data['username'])->first()) {
                return ApiResponse::error('用户名已存在');
            }

            $res = [
                'username'   => $data['username'],
                'nickname'   => $data['nickname'],
                'password'   => Hash::make($data['username']),
                'avatar'     => empty($data['avatar']) ? 'https://guzhong1.oss-cn-shenzhen.aliyuncs.com/%E5%8D%A1%E5%88%B8%E5%B0%8F%E7%A8%8B%E5%BA%8F/%E4%B8%AA%E4%BA%BA%E4%B8%AD%E5%BF%83/%E5%A4%B4%E5%83%8F.jpg' : $data['avatar'],
                'status'     => $data['status'],
                'role_id'    => $data['role_id'],
                'created_at' => Carbon::now(),
            ];
            $save = AdminUserModel::insert($res);

        } else {

            $res = [
                'username'   => $data['username'],
                'nickname'   => $data['nickname'],
                'avatar'     => empty($data['avatar']) ? 'https://guzhong1.oss-cn-shenzhen.aliyuncs.com/%E5%8D%A1%E5%88%B8%E5%B0%8F%E7%A8%8B%E5%BA%8F/%E4%B8%AA%E4%BA%BA%E4%B8%AD%E5%BF%83/%E5%A4%B4%E5%83%8F.jpg' : $data['avatar'],
                'status'     => $data['status'],
                'role_id'    => $data['role_id'],
                'updated_at' => Carbon::now(),
            ];
            $save = AdminUserModel::where('id', $data['id'])->update($res);

        }

        if ($save) {
            return ApiResponse::success('管理员表单提交成功');
        } else {
            return ApiResponse::error('管理员表单提交失败');
        }
    }


}
